package com.study.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study.book.common.AuthenticationRequired;
import com.study.book.common.R;
import com.study.book.dto.OrderDto;
import com.study.book.mapper.LibrarySeatDetailMapper;
import com.study.book.pojo.*;
import com.study.book.service.LibraryService;
import com.study.book.service.OrderService;
import com.study.book.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "订单接口")
@RestController
@RequestMapping("/app/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @Autowired
    private LibraryService libraryService;

    @Autowired
    private LibrarySeatDetailMapper librarySeatDetailMapper;


    @ApiOperation("获取所有订单列表")
    @GetMapping("/list")
    public R<List<Order>> getOrderListForUserId(){
        List<Order> orders = orderService.list();
        orders.replaceAll(e->{
            Product product = productService.getById(e.getProductid());
            e.setProductName(product.getProductname());
            e.setPhoto(product.getPhoto());
            return e;});
        return R.ok(orders);
    }


    @ApiOperation("根据用户id获取订单列表")
    @AuthenticationRequired(value = "0")
    @GetMapping("/getOrderListForUserId")
    public R<List<Order>> getOrderListForUserId(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        List<Order> orders = orderService.getOrderListForUserId(Integer.parseInt(userId));
        orders.replaceAll(e->{
            Product product = productService.getById(e.getProductid());
            e.setProductName(product.getProductname());
            e.setPhoto(product.getPhoto());
            return e;});
        return R.ok(orders);
    }

    @ApiOperation("根据订单id获取订单列表")
    @GetMapping("/getOrderDetails/{orderNumber}")
    public R<Order> getOrderDetails(@PathVariable("orderNumber") String orderNumber){
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Order::getOrdernumber,orderNumber);

        Order order = orderService.list(lambdaQueryWrapper).get(0);
        Product product = productService.getById(order.getProductid());
        order.setProductName(product.getProductname());
        order.setPhoto(product.getPhoto());
        order.setPrice(product.getPrice());
        order.setLibraryName(libraryService.getById(order.getLibraryId()).getLibraryname());
        return R.ok(order);
    }

    @ApiOperation("商品订单评论")
    @PostMapping("/orderReview")
    public R orderReview(@RequestBody OrderDto dto){
        return R.ok(orderService.orderReview(dto));
    }

    @ApiOperation("生成订单")
    @AuthenticationRequired(value = "0")
    @PostMapping("/creatOrder")
    public R creatOrder(HttpServletRequest request,@RequestBody OrderDto dto){
        String id = (String) request.getAttribute("userId");
        return orderService.creatOrder(id,dto);
    }

    @ApiOperation("图书馆预定订单评论")
    @GetMapping("/libraryComment/{id}/{comment}/{star}")
    public R orderReview(@PathVariable("id") Integer id,@PathVariable("comment") String comment,@PathVariable("star") Integer star){
        LibrarySeatDetail librarySeatDetail = librarySeatDetailMapper.selectById(id);
        librarySeatDetail.setFlag("2");
        librarySeatDetail.setComment(comment);
        librarySeatDetail.setStar(star);
        return R.ok(librarySeatDetailMapper.updateById(librarySeatDetail));
    }

    @ApiOperation("根据用户id获取图书馆预定列表")
    @AuthenticationRequired(value = "0")
    @GetMapping("/getListForUserId")
    public R<List<LibrarySeatDetail>> getListForUserId(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        LambdaQueryWrapper<LibrarySeatDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LibrarySeatDetail::getUserId,Integer.parseInt(userId));
        List<LibrarySeatDetail> orders = librarySeatDetailMapper.selectList(lambdaQueryWrapper);
        orders.replaceAll(e->{
            Library library = libraryService.getById(e.getLibraryid());
            e.setLibraryName(library.getLibraryname());
            e.setPhoto(library.getPhoto());
            return e;});
        return R.ok(orders);
    }

    @ApiOperation("根据预定id获取图书馆预定信息")
    @GetMapping("/getListById/{id}")
    public R<LibrarySeatDetail> getListById(@PathVariable("id") Integer id){
        LibrarySeatDetail orders = librarySeatDetailMapper.selectById(id);
            Library library = libraryService.getById(orders.getLibraryid());
        orders.setLibraryName(library.getLibraryname());
        orders.setPhoto(library.getPhoto());
        orders.setLibraryAddress(library.getAddress());
        return R.ok(orders);
    }
}
